package com.justgame.project.module;

import com.baomidou.mybatisplus.extension.api.R;
import com.justgame.project.generator.enums.Rule;
import com.justgame.project.generator.schema.Entity;
import com.justgame.project.generator.schema.Enumerate;
import com.justgame.project.generator.schema.IModule;
import com.justgame.project.generator.schema.Service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.justgame.project.generator.schema.Field.*;
import static com.justgame.project.generator.schema.Join.leftJoin;
import static com.justgame.project.generator.schema.Service.AddBy.Param.column;
import static com.justgame.project.generator.schema.Service.AddBy.Param.init;

@Configuration
public class SystemModule implements IModule {
    @Override
    public String getModule() {
        return "system";
    }

    @Bean
    public Entity sysUserInfo(){
        return Entity.baseEntityBuilder(getModule(),"SysUserInfo","用户信息表")
                .fields(
                        string("userName","账号"),
                        encrypt("password","密码"),
                        type("sex","性别"),
                        type("status","状态"),
                        string("email","电子邮箱"),
                        string("phoneNumber","手机号"),
                        string("openId","微信授权id"),
                        type("role","角色"),
                        dateTime("lastAccessDate","最后访问日期")
                )
                .joinTables(
                        leftJoin("userSocialInfo","id","SysUserSocialInfo","userInfoId"),
                        leftJoin("userAccount","id","SysUserAccount","userInfoId")
                )
                .enums(
                        Enumerate.builder("sex")
                                .item("MALE","男性")
                                .item("FEMALE","女性")
                                .item("UNKNOWN","未知"),
                        Enumerate.builder("status")
                                .item("HEALTH","健康")
                                .item("LOCKED","封禁"),
                        Enumerate.builder("role")
                                .item("USER","普通用户")
                                .item("VIP_USER","会员用户")
                                .item("ADMIN","超级管理员")
                )
                .service(
                        Service.builder()
                                .addBy(
                                        Service.AddBy.builder()
                                                .name("wechatUserRegister")
                                                .description("注册账号")
                                                .params(
                                                        column("userName").rule(Rule.notNull()),
                                                        column("password").rule(Rule.notNull()),
                                                        column("phoneNumber").rule(Rule.phone()),
                                                        column("openId").rule(Rule.notNull()),
                                                        init("status","HEALTH"),
                                                        init("sex","UNKNOWN"),
                                                        init("role","USER"),
                                                        init("lastAccessDate","LocalDateTime.now()")
                                                )
                                                .build()
                                )
                                .findBy(
                                        Service.FindBy.builder()
                                                .description("通过邮箱获取用户信息")
                                                .is("email",Rule.email())
                                                .build()
                                )
                                .findBy(
                                        Service.FindBy.builder()
                                                .description("通过邮箱获取用户信息")
                                                .is("phoneNumber",Rule.phone())
                                                .build()
                                )
                                .updateBy(
                                        Service.UpdateBy.builder()
                                                .description("更新邮箱")
                                                .param("email",Rule.email())
                                                .build()
                                )
                                .updateBy("openId")
                                .findByIs("userName","password")
                                .findByIs("openId")
                                .countByIs("userName")
                                .countByIs("email")
                                .countByIs("phoneNumber")
                                .countByIs("openId")
                                .build()
                )
                .build();
    }

    @Bean
    public Entity SysUserSocialInfo(){
        return Entity.baseEntityBuilder(getModule(),"SysUserSocialInfo","用户社交信息表")
                .fields(
                        string("nickname","昵称"),
                        url("avatar","头像"),
                        string("qq","QQ账号"),
                        string("wechatNo","微信账号"),
                        bool("isPrivate","账号敏感社交信息是否隐藏"),
                        key("userInfoId","用户信息"),
                        text("description","简介")
                )
                .service(
                        Service.builder()
                                .addBy(Service.AddBy.builder()
                                        .name("addByUserRegister")
                                        .description("用户注册后调用创建用户社交信息")
                                        .params(
                                                column("nickname").rule(Rule.notNull()),
                                                column("userInfoId").rule(Rule.notNull()),
                                                init("isPrivate","false")
                                        )
                                        .build())
                                .findByIs("userInfoId")
                                .updateBy("nickname")
                                .updateBy("avatar")
                                .countByIs("nickname")
                                .build()
                )
                .build();
    }

    @Bean
    public Entity SysUserAccount(){
        return Entity.baseEntityBuilder(getModule(),"SysUserAccount","用户账户信息表")
                .fields(
                        key("userInfoId","用户信息"),
                        decimal("coin","硬币"),
                        decimal("integral","积分"),
                        decimal("rmb","账户人民币余额")
                )
                .service(
                        Service.builder()
                                .addBy(Service.AddBy.builder()
                                        .name("addByUserRegister")
                                        .description("用户注册后调用创建用户账户信息")
                                        .params(
                                                column("userInfoId").rule(Rule.notNull()),
                                                column("coin").defaultValue("BigDecimal.ZERO"),
                                                column("integral").defaultValue("BigDecimal.ZERO"),
                                                init("rmb","BigDecimal.ZERO")
                                        )
                                        .build())
                                .findByIs("userInfoId")
                                .build()
                )
                .build();
    }

    @Bean
    public Entity SysDict(){
        return Entity.baseEntityBuilder(getModule(),"SysDict","系统字典")
                .fields(
                        key("parentId","父级id"),
                        string("code","字典编号"),
                        string("key","键"),
                        string("value","值"),
                        string("remark","备注")
                )
                .joinTables(
                        leftJoin("parent","parentId","SysDict","id")
                )
                .build();
    }

    @Bean
    public Entity SysFileInfo(){
        return Entity.baseEntityBuilder(getModule(),"SysFileInfo","系统文件信息存储表")
                .fields(
                        string("name","文件名"),
                        string("path","存储路径"),
                        string("size","文件大小（bite字节）"),
                        string("suffix","文件后缀"),
                        type("accessLevel","访问权限"),
                        string("lockPassword","文件访问密码"),
                        bool("willDel","是否需要预删除（未来的某一时间会删除，因此暂时判定为已删除。定时器会自动处理删除这个动作）")
                )
                .enums(
                        Enumerate.builder("accessLevel")
                                .item("PUBLIC","公共权限")
                                .item("AUTH","需要验证身份")
                                .item("PRIVATE","私密权限")
                )
                .service(
                        Service.builder()
                                .listBy(Service.ListBy.builder().in("path").build())
                                .build()
                )
                .build();
    }

    @Bean
    public Entity SysHomeBanner(){
        return Entity.baseEntityBuilder(getModule(),"SysHomeBanner","首页轮播信息表")
                .fields(
                        string("title","标题"),
                        string("description","描述"),
                        url("coverFile","封面图"),
                        url("link","链接地址"),
                        integer("order","排序")
                )
                .build();
    }

    @Bean
    public Entity SysHomeColumn(){
        return Entity.baseEntityBuilder(getModule(),"SysHomeColumn","首页菜单栏目配置表")
                .fields(
                        string("title","标题"),
                        type("logoType","板块类型"),
                        url("linkPath","链接地址"),
                        text("description","描述")
                )
                .enums(
                        Enumerate.builder("logoType")
                )
                .build();
    }

    @Bean
    public Entity SysPlatformInfo(){
        return Entity.baseEntityBuilder(getModule(),"SysPlatformInfo","平台类型信息表")
                .fields(
                        string("name","名称"),
                        string("tag","标记")
                )
                .build();
    }

    @Bean
    public Entity SysStationOrder(){
        return Entity.baseEntityBuilder(getModule(),"SysStationOrder","平台站内订单")
                .fields(
                        integer("gameInfoId","游戏信息"),
                        decimal("costCoins","购买消耗金币"),
                        decimal("beforePaidCoins","支付前拥有金币"),
                        dateTime("expireDate","订单支付过期时间"),
                        string("orderNo","订单编号"),
                        decimal("costIntegral","购买消耗积分"),
                        decimal("beforePaidIntegral","支付前拥有积分"),
                        string("remark","备注")
                )
                .build();
    }

    @Bean
    public Entity SysUserOperationLog(){
        return Entity.builder(getModule(),"SysUserOperationLog","用户功能操作间隔限制表")
                .fields(
                        key("userInfoId","用户信息"),
                        type("operation","操作类型"),
                        dateTime("startDate","操作开始日期"),
                        dateTime("endDate","结束限制日期")
                )
                .enums(
                        Enumerate.builder("operation")
                                .item("UPDATE_AVATAR","用户修改头像")
                                .item("UPDATE_NICKNAME","用户修改昵称")
                                .item("SEND_AUTH_SMS","发送短信认证")
                                .item("SEND_AUTH_MAIL","发送邮箱认证")
                                .item("UPDATE_PHONE","修改手机号绑定")
                                .item("UPDATE_MAIL","修改邮箱绑定")
                )
                .service(
                        Service.builder()
                                .addBy(
                                        Service.AddBy.builder()
                                                .name("saveOperationLog")
                                                .description("保存操作限制记录")
                                                .params(
                                                        column("userInfoId").rule(Rule.notNull()),
                                                        column("operation").rule(Rule.notNull()),
                                                        column("endDate").rule(Rule.notNull()),
                                                        init("startDate","LocalDateTime.now()")
                                                )
                                                .build()
                                )
                                .countBy(
                                        Service.FindBy.builder()
                                                .name("hasRestrict")
                                                .description("查询是否被限制操作")
                                                .is("userInfoId",Rule.notNull())
                                                .is("operation",Rule.notNull())
                                                .greaterThan("endDate",Rule.notNull())
                                                .build()
                                )
                                .findByIs("userInfoId","operation")
                                .updateBy("startDate","endDate")
                                .build()
                )
                .build();
    }
}
